# -*- coding: utf-8 -*-
# @Author  : pony.guan
# @Time    : 2021/10/18
# Description:

import time
import json
import random
from dwebsocket.decorators import accept_websocket

TASK_FLOW_DIC = {
    "out": {
        "container_code": random.choice(range(1, 100))
    }
}


class TaskFlow:
    flow_dic = {
        'out': '_out_flow',
        'in': '_in_flow'
    }

    @staticmethod
    def _out_flow(self):
        """
             t1          t2                 t3              t4              t5                t6
        开始----->下发任务--->通知机器人执行任务---->机器人到达库位--->机器人取走容器---->机器人到达工作站--->机器人把容器放到了工作站--->完成
        :param self:
        :return:
        """

        robot_id = 'robot' + str(random.choice(range(1, 5)))
        container_code = 'container00' + str(random.choice(range(1, 100)))
        location_code = 'H' + \
                        '-' + str(random.choice(range(1, 15))) + \
                        '-' + str(random.choice(range(1, 50))) + \
                        '-' + str(random.choice(range(1, 20)))
        station_code = 'station' + str(random.choice(range(1, 2)))

        task_duration_lst = [
            {'task_name': '下发任务', 'duration': 0.001},
            {'task_name': f'通知机器人{robot_id}执行任务', 'duration': 0.002},
            {'task_name': f'机器人{robot_id}到达库位{location_code}', 'duration': random.choice(range(1, 5))},
            {'task_name': f'机器人{robot_id}取走容器{container_code}', 'duration': random.choice(range(1, 5))},
            {'task_name': f'机器人{robot_id}到达工作站{station_code}', 'duration': random.choice(range(1, 5))},
            {'task_name': f'机器人{robot_id}把容器放到了工作站{station_code}上', 'duration': random.choice(range(1, 5))},
        ]

        return {
            'robot_id': robot_id,
            'container_code': container_code,
            'location_code': location_code,
            'station_code': station_code,
            'task_duration_lst': task_duration_lst
        }

    @staticmethod
    def _in_flow(self):
        pass

    # todo: 盘库和理库

    def crate_flow(self, task_type):
        ret = getattr(self, self.flow_dic[task_type])()
        return ret


@accept_websocket
def task_flow(request):
    if request.is_websocket():
        print('创建ws连接....')
        msg = request.websocket.wait()  # 接收前端发来的消息
        print('接受到了消息')
        task_type = json.loads(msg)
        print('开始推送消息')
        ret = TaskFlow().crate_flow(task_type)
        robot_id = ret['robot_id']
        station_code = ret['station_code']
        task_duration_lst = ret['task_duration_lst']
        finished_task_name_lst = ['开始']
        for index, task_duration in enumerate(task_duration_lst):
            task_name, duration = task_duration.items()
            time.sleep(duration)
            finished_task_name_lst.append(task_name)
            if index == len(task_duration_lst) - 1:
                finished_task_name_lst.append(f'机器人{robot_id}把容器放到了工作站{station_code}')
                finished_task_name_lst.append('完成')
            request.websocket.send(json.dumps({
                'code': 1000,
                'data': finished_task_name_lst
            }, ensure_ascii=False))
        request.is_websocket().close()
# ws = new WebSocket('ws://172.20.3.141:8888/api/v1/system_demo/ws_app/task_flow/')
# ws.onmessage = event => console.log(event.data)
# ws.send('hello')
